package com.sskj.mine.ui.activity;


import android.Manifest;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ImgUtil;
import com.sskj.common.util.PermissionTipUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.CopyUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.QrCodeBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.ExtensionActivityPresenter;
import com.tbruyelle.rxpermissions2.RxPermissions;

import javax.inject.Inject;

import butterknife.BindView;


/**
 * 我要推广
 */
@Route(path = RConfig.MINE_WYTG)//我要推广
public class ExtensionActivity extends BaseActivity<ExtensionActivityPresenter> {


    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.ivQRCode)
    ImageView ivQRCode;
    @BindView(R2.id.tvInviteUrl)
    TextView tvInviteUrl;
    @BindView(R2.id.tvCopy)
    TextView tvCopy;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_extension;
    }

    @Override
    public ExtensionActivityPresenter getPresenter() {
        return new ExtensionActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        setTitle("我的邀请");
        // 长按保存图片

        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                UserData userData = users.get(0);

            }
        });
    }

    @Override
    protected void initData() {
        mPresenter.getUrlData();
    }

    // 接口请求成功调用函数
    public void onSuccessUi(QrCodeBean qrCodeBean) {
        ImageUtil.setImage(qrCodeBean.getQrc(), ivQRCode);
        tvInviteUrl.setText(qrCodeBean.getUrl());
        ClickUtil.click(tvCopy, () -> {  // 保存Code值
            CopyUtil.copy(qrCodeBean.getUrl());
        });
        ivQRCode.setOnLongClickListener(v -> {
            new RxPermissions(ExtensionActivity.this)
                    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(isGrant -> {
                        if (isGrant) {
                            ImgUtil.saveImageToGallery(ExtensionActivity.this, v);
                        } else {
                            PermissionTipUtil.tip(ExtensionActivity.this, "存储");
                        }
                    });
            return false;
        });
    }
}